package com.igeekhome.microserviceconsumermovie8001.controller;

import com.igeekhome.microservicecommon.feign.UserFeignService;
import com.igeekhome.microservicecommon.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * 服务消费者：查询用户信息
 * @author yadonghe
 * @date 2020-02-21 10:39
 */
@RestController
@RequestMapping("/movie")
public class MovieController {

    /**
     * Spring提供的同步的HTTP请求执行工具 ，可以执行GET\POST\PUT\DELETE...
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserFeignService userFeignService;

    @RequestMapping("/get/user/{id}")
    public User get(@PathVariable Long id) {
        //不需要自己编写service、mapper查询用户信息
        //http://localhost:8081/user/get/1
        //使用Java代码发起HTTP请求
        //Spring提供了RestTemplate 用于执行HTTP请求
        //参数：1.请求的URL地址 2.需要将接口返回值转换的类型
       // User user = restTemplate.getForObject("http://MICROSERVICE-PROVIDER-USER/user/get/{id}", User.class,id);
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return userFeignService.get(id);
    }


//    public User findByIdFallback(Long id) {
//        return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
//    }

}
